﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using VideoClipNameHelper;

namespace Videos2Links
{
    partial class Program
    {
        //================================================================
        //================================================================
        //
        private static void byArtist()
        {
            // BY ARTIST
            //
            Console.WriteLine("@ByArtist");
            RecreateDirectory(cp.ByArtistDir);
            treeData.Add("#");
            treeData.Add("1|D|ByArtist");

            //var byArtist = (from vf in sourceFiles select vf).GroupBy(x => x.vcd.Artist.ToUpperInvariant());
            //
            foreach (var allClipsForOneArtist in clipsGroupedByArtist)
            {
                var distinctClipsForOneArtist = (from x in allClipsForOneArtist select x).Distinct();
                if (distinctClipsForOneArtist.Count() > 0)
                {
                    createLinksByArtist(cp.ByArtistDir, distinctClipsForOneArtist);
                    //
                    treeData.Add($"2|C|{distinctClipsForOneArtist.First().vcd.Artist}|true");
                    foreach(var vf in distinctClipsForOneArtist)
                    {
                        treeData.Add($"3|F|{vf.PathName}|");
                    }
                    //
                }
            }
        }
        //
        //================================================================
        //================================================================
        //
        private static void byAlbum()
        {
            //
            // BY ALBUM
            //
            Console.WriteLine("@ByAlbum");
            RecreateDirectory(cp.ByAlbumDir);
            treeData.Add("#");
            treeData.Add("1|D|ByAlbum");
            //
            //
            foreach (var allClipsForOneArtistAlbum in clipsGroupedByArtistAlbum)
            {
                var distinctClipsForOneArtist = (from x in allClipsForOneArtistAlbum select x).Distinct();
                if (distinctClipsForOneArtist.Count() > 0)
                {
                    createLinksByAlbum(cp.ByAlbumDir, distinctClipsForOneArtist);
                    //
                    treeData.Add($"2|C|{distinctClipsForOneArtist.First().vcd.Artist}|false");
                    foreach(var vf in distinctClipsForOneArtist)
                    {
                        treeData.Add($"3|F|{vf.PathName}|");
                    }
                    //
                }
            }
        }
        //
        //================================================================
        //================================================================
        //
        private static void byTitle()
        {
            //
            // BY TITLE
            //
            Console.WriteLine("@ByTitle");
            RecreateDirectory(cp.ByTitleDir);
            treeData.Add("#");
            treeData.Add("1|D|ByTitle");
            //
            vfComprerFull vfC = new vfComprerFull();
            //
            foreach (var allClipsWithSameTitle in clipsGroupedByTitle)
            {
                //var ff = (from x in y select x).Distinct(vfC).ToArray();
                var ff = (from x in allClipsWithSameTitle select x).Distinct(vfC);
                createLinksByTitle(cp.ByTitleDir, ff);
                //
                if (ff.Count() > 1)
                {
                    treeData.Add($"2|C|{ff.First().vcd.Title}|true");
                    foreach(var vf in ff)
                    {
                        treeData.Add($"3|F|{vf.PathName}|");
                    }
                    //
                }
                else
                {
                    treeData.Add($"2|C|{ff.First().vcd.Title}|false");
                    onlyOneTitle.Add(ff.First().vcd.Title + " - " + ff.First().vcd.Artist);
                    //
                    treeData.Add($"3|F|{ff.First().PathName}|");
                    //
                }
            }
        }
        //
        //================================================================
        //================================================================
        //
        private static void byYear()
        {
            //
            // BY YEAR
            //
            Console.WriteLine("@ByYear");
            RecreateDirectory(cp.ByYearDir);
            treeData.Add("#");
            treeData.Add("1|D|ByYear");
            /*
            //
            var clipsGroupedByYear = (from vf in sourceFiles where 0 != vf.vcd.Year orderby vf.vcd.Year select vf).GroupBy(x => x.vcd.Year);
            foreach (var inYearAllClips in clipsGroupedByYear)
            {
                //var ff = (from x in inYearAllClips select x).Distinct(vfC).ToArray();
                string dstDir = Path.Combine(byYearDirectory, inYearAllClips.Key.ToString());
                foreach (var inYearClip in inYearAllClips)
                {
                    createLinkByAll(dstDir, inYearClip.FileNameWithoutExtension, inYearClip);
                }
                //
                if (inYearAllClips.Count() > 1)
                {
                    writeHintFile(dstDir, true);
                }
            }
            */
            byYear(1950, 1960);
            byYear(1960, 1970);
            byYear(1970, 1980);
            byYear(1980, 1990);
            byYear(1990, 2000);
            byYear(2010);
            byYear(2011);
            byYear(2012);
            byYear(2013);
            byYear(2014);
            byYear(2015);
            byYear(2016);
        }
        //
        //================================================================
        //================================================================
        //
        private static void byYear(int yearSelected)
        {
            byYear(yearSelected, yearSelected);
        }

        private static void byYear(int yearFirst, int yearLast)
        {
            //
            string dirName = "";
            if (yearFirst == yearLast) dirName = $"{yearFirst}";
            else dirName = $"{yearFirst}-{yearLast}";
            Console.WriteLine($"\t{dirName}");
            //-------------------------------------
            //
            var clipsGrouped = from vf in sourceVideoFiles where vf.vcd.Year >= yearFirst && vf.vcd.Year <= yearLast select vf;
            string dstDir = Path.Combine(cp.ByYearDir, dirName);
            treeData.Add($"2|C|{dirName}|{(clipsGrouped.Count() > 1)}");
            foreach (VideoFile vf in clipsGrouped)
            {
                createLinkByAll(dstDir, vf.FileNameWithoutExtension(), vf);
                //
                treeData.Add($"3|F|{vf.PathName}|");
                //
            }
            if (clipsGrouped.Count() > 1)
            {
                _WriteHintFile(dstDir, true);
            }
        }
        //
        //================================================================
        //================================================================
        //
        private static void byNew()
        {
            //
            // BY NEW
            //
            Console.WriteLine("@ByNew");
            RecreateDirectory(cp.ByNewerDir);
            treeData.Add("#");
            treeData.Add("1|C|ByNew|false");
            //
            var byNewer = ((from vf in sourceVideoFiles select vf).OrderByDescending(x => x.ModificationDT)).Take(100).ToList();
            for (int n = 0; n < byNewer.Count(); n++)
            {
                string ln = string.Format("{0:000} {1} - {2}", n, byNewer[n].vcd.Artist, byNewer[n].vcd.Title);
                createLinkByAll(cp.ByNewerDir, ln, byNewer[n]);
                //
                treeData.Add($"2|F|{byNewer[n].PathName}|");
                //
            }
        }
        //
        //================================================================
        //================================================================
        //
        private static void byLiked()
        {
            // BY LIKED
            //
            Console.WriteLine("@ByLiked");
            RecreateDirectory(cp.ByLikedDir);
            _WriteHintFile(cp.ByLikedDir, true);
            treeData.Add("#");
            treeData.Add("1|C|ByLiked|true");
            //
            //
            List<string> liked = VideoClipNameHelper.CDBHandler.GetLiked();
            //
            IEnumerable<VideoFile> byLiked = sourceVideoFiles.Where(r => liked.Contains(r.vcd.Concat(true)));
            foreach (var li in byLiked)
            {
                createLinkByAll(cp.ByLikedDir, li.FileNameWithoutExtension(), li);
                //
                treeData.Add($"2|F|{li.PathName}");
            }
        }
        //
        //================================================================
        //
        private static void bySpecialFolders()
        {
            //
            // BY SPECIAL FOLDERS
            //
            if (0 != cp.SpecialListsPathnames.Length)
            {
                Console.WriteLine("@SpecialLists");
                RecreateDirectory(cp.SpecialListsDir);
                treeData.Add("#");
                treeData.Add("1|D|SpecialLists");
                //
                CSpecialFolder csf = new CSpecialFolder(sourceFilesExpanded, ConfigData.GetFullPath(ConfigData.KeyName.DestinationRoot));
                foreach (string spc in cp.SpecialListsPathnames)
                {
                    try
                    {
                        Console.WriteLine("\t" + System.IO.Path.GetFileNameWithoutExtension(spc));
                        csf.ReadConfigFile(spc);
                        // 
                        string dstDir = Path.Combine(cp.SpecialListsDir, Path.GetFileNameWithoutExtension(csf.DestinationDir));
                        RecreateDirectory(dstDir);
                        treeData.Add($"2|C|{Path.GetFileNameWithoutExtension(csf.DestinationDir)}|{(csf.foundClips.Count > 0)}");
                        //
                        if (csf.foundClips.Count > 0)
                        {
                            KeyValuePair<string, string> kv = GeneralConfig.GetKeyValuePair(csf.configData, "@SourceFile");
                            _WriteHintFile(dstDir, GeneralConfig.GetValueBool(csf.configData, "random"), kv);
                        }
                        //
                        //Parallel.ForEach(csf.foundClips, (vfd) =>
                        foreach (VideoFile vfd in csf.foundClips)
                        {
                            createLinkByAll(dstDir, vfd.FileNameWithoutExtension(), vfd);
                            //
                            treeData.Add($"3|F|{vfd.PathName}");
                            //
                            }
                        //});
                        //
                    }
                    catch (Exception e)
                    {
                        string eMsg = string.Format("ERROR createLinkByArtist {0},{1},{2}", e.Message, e.Source, e.InnerException);
                        errList.Add(eMsg);
                        Console.WriteLine(eMsg);
                    }
                    finally
                    {
                        if (csf.errList.Count() > 0)
                        {
                            errList.AddRange(csf.errList);
                        }
                    }
                }
                Console.WriteLine();
            }
        }
        //
        //================================================================
        //================================================================

    }//class
}//namespace
